{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "df7ec903",
   "metadata": {},
   "source": [
    "________________\n",
    "\n",
    "* 2022/10/24/week10\n",
    "* Python学习记录\n",
    "_________________"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93dfd85f",
   "metadata": {},
   "source": [
    "# 本周学习计划\n",
    "\n",
    "> 1. while循环\n",
    "> 2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "678e6171",
   "metadata": {},
   "source": [
    "## 1. while循环\n",
    "* 回顾：for循环 ：for + range()可以指定循环的次数-->关于网页URL队列的抓取实践\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "* while True：无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddd035e7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7d666edb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯... 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 9\n"
     ]
    }
   ],
   "source": [
    "# 1. 指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯...\",count)\n",
    "    count = count+1\n",
    "    if count == 10:\n",
    "        break # break 终止循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d739b1d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号: admin\n",
      "密码: 123456\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[1;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# 2. while True循环，可以让用户重复输入\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# user01 = \"admin\"\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# password01 = \"123456\"\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m----> 6\u001b[0m     username \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m账号: \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      7\u001b[0m     password \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m密码: \u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32mD:\\Users\\pc\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py:1075\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m   1071\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_allow_stdin:\n\u001b[0;32m   1072\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(\n\u001b[0;32m   1073\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1074\u001b[0m     )\n\u001b[1;32m-> 1075\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   1076\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1077\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1078\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1079\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m   1080\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\Users\\pc\\anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py:1120\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m   1117\u001b[0m             \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m   1118\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m   1119\u001b[0m     \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[1;32m-> 1120\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[0;32m   1121\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m   1122\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "# 2. while True循环，可以让用户重复输入\n",
    "# user01 = \"admin\"\n",
    "# password01 = \"123456\"\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号: \")\n",
    "    password = input(\"密码: \")\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "adaed307",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号: ghjjj\n",
      "密码: 33333\n",
      "账号不存在或密码不正确!请重新输入!您还有2尝试的机会\n",
      "账号: ghjl\n",
      "密码: 44444\n",
      "账号不存在或密码不正确!请重新输入!您还有1尝试的机会\n",
      "账号: ggsaa3\n",
      "密码: 55786\n",
      "账号不存在或密码不正确!请一小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "count = 0 # 计数器\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号: \")\n",
    "    password = input(\"密码: \")\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登陆成功~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    # 情景二：账号不存在或密码不正确，用户可以输入三次，三次之后一小时之后再来尝试\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号不存在或密码不正确!请重新输入!您还有\"+str(2-count)+\"尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确!请一小时后再来尝试\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca170559",
   "metadata": {},
   "source": [
    "## 2. 购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1340c189",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "15243b61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________商品列表___________\n",
      "蜜雪柠檬水 6元\n",
      "蜜桃四季春 10元\n",
      "芒果奶盖 12元\n",
      "葡萄啵啵奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额✔, 然后打印我们的商品列表给用户。✔\n",
    "\n",
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "print(\"_________商品列表___________\")\n",
    "for i in range(4):\n",
    "    print(product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "# print(product_list[0][0],str(product_list[0][1])+\"元\") # 格式化输出\n",
    "# print(product_list[1][0],str(product_list[1][1])+\"元\")\n",
    "# print(product_list[2][0],str(product_list[2][1])+\"元\")\n",
    "# print(product_list[3][0],str(product_list[3][1])+\"元\")\n",
    "\n",
    "# Alipay = input(\"请输入余额:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b8541bd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:1000\n",
      "_________商品列表___________\n",
      "蜜雪柠檬水 6元\n",
      "蜜桃四季春 10元\n",
      "芒果奶盖 12元\n",
      "葡萄啵啵奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "Alipay = input(\"请输入余额:\")\n",
    "print(\"_________商品列表___________\")\n",
    "for i in range(4):\n",
    "    print(product_list[i][0],str(product_list[i][1])+\"元\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "255e8a39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:1000\n",
      "_________商品列表___________\n",
      "商品序号 (0):  蜜雪柠檬水 6元\n",
      "商品序号 (1):  蜜桃四季春 10元\n",
      "商品序号 (2):  芒果奶盖 12元\n",
      "商品序号 (3):  葡萄啵啵奶茶 8元\n"
     ]
    }
   ],
   "source": [
    "# 2. 让用户输入商品编号进行商品的购买。:直接用商品列表的 index 作为商品编号\n",
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "Alipay = input(\"请输入余额:\")\n",
    "print(\"_________商品列表___________\")\n",
    "for i in range(4):\n",
    "    print(\"商品序号\",\"(\"+str(i)+\"): \",product_list[i][0],str(product_list[i][1])+\"元\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "df321179",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:100\n",
      "_________商品列表___________\n",
      "商品序号 (0):  蜜雪柠檬水 6元\n",
      "商品序号 (1):  蜜桃四季春 10元\n",
      "商品序号 (2):  芒果奶盖 12元\n",
      "商品序号 (3):  葡萄啵啵奶茶 8元\n",
      "请输入商品编号:2\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [49]\u001b[0m, in \u001b[0;36m<cell line: 13>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     11\u001b[0m user_choice \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m请输入商品编号:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     12\u001b[0m \u001b[38;5;66;03m# 情景一: 余额大于商品价格\u001b[39;00m\n\u001b[1;32m---> 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m Alipay \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[43mproduct_list\u001b[49m\u001b[43m[\u001b[49m\u001b[43muser_choice\u001b[49m\u001b[43m]\u001b[49m[\u001b[38;5;241m1\u001b[39m]:\n\u001b[0;32m     14\u001b[0m    \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m商品购买成功，正在制作中...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m# 情景二: 余额小于商品价格\u001b[39;00m\n\u001b[0;32m     16\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
      "\u001b[1;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "Alipay = input(\"请输入余额:\")\n",
    "print(\"_________商品列表___________\")\n",
    "for i in range(4):\n",
    "    print(\"商品序号\",\"(\"+str(i)+\"): \",product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "user_choice = input(\"请输入商品编号:\")\n",
    "# 情景一: 余额大于商品价格\n",
    "if Alipay >= product_list[user_choice][1]:\n",
    "   print(\"商品购买成功，正在制作中...\")\n",
    "# 情景二: 余额小于商品价格\n",
    "else:\n",
    "   print(\"您的余额不足，请更换支付方式~~~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "522c95ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:100\n",
      "--------商品列表---------\n",
      "商品序号 (0):  蜜雪柠檬水 6元\n",
      "商品序号 (1):  蜜桃四季春 10元\n",
      "商品序号 (2):  芒果奶盖 12元\n",
      "商品序号 (3):  葡萄啵啵奶茶 8元\n",
      "请输入商品编号:2\n",
      "商品购买成功，正在制作中...\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "Alipay = int(input(\"请输入余额:\"))\n",
    "print(\"--------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(\"商品序号\",\"(\"+str(i)+\"): \",product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "user_choice = input(\"请输入商品编号:\")\n",
    "# 情景一: 余额大于商品价格\n",
    "if Alipay >= product_list[int(user_choice)][1]:\n",
    "    print(\"商品购买成功，正在制作中...\")\n",
    "# 情景二: 余额小于商品价格\n",
    "else:\n",
    "    print(\"您的余额不足，请更换支付方式~~~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5558d041",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:100\n",
      "--------商品列表---------\n",
      "商品序号 (0):  蜜雪柠檬水 6元\n",
      "商品序号 (1):  蜜桃四季春 10元\n",
      "商品序号 (2):  芒果奶盖 12元\n",
      "商品序号 (3):  葡萄啵啵奶茶 8元\n",
      "请输入商品编号:2\n",
      "商品购买成功，正在制作中...\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8]\n",
    "]\n",
    "Alipay = int(input(\"请输入余额:\"))\n",
    "             \n",
    "print(\"--------商品列表---------\")\n",
    "for i in range(4):\n",
    "    print(\"商品序号\",\"(\"+str(i)+\"): \",product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "user_choice = input(\"请输入商品编号:\")\n",
    "# 情景一: 余额大于商品价格\n",
    "if Alipay >= product_list[int(user_choice)][1]:\n",
    "    print(\"商品购买成功，正在制作中...\")\n",
    "# 情景二: 余额小于商品价格\n",
    "else:\n",
    "    print(\"您的余额不足，请更换支付方式~~~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "715744b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_list = [\n",
    "   [ \"蜜雪柠檬水\",6],\n",
    "   [\"蜜桃四季春\",10],\n",
    "   [\"芒果奶盖\",12],\n",
    "   [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"--------------商品列表--------------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\"):\" ,product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "\n",
    "    # 2.让用户输入商品编号进行购买：直接用商品列表的index作为商品编号\n",
    "\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景一：余额大于商品价格\n",
    "    if Alipay >= product_list[int(user_choice)][1]:\n",
    "        Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "        print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "\n",
    "    # 情景二：余额小于商品价格\n",
    "    else:\n",
    "        print(\"您的余额不足，请更换支付方式~~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "cc7e6b41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：100\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蜜雪柠檬水 6元\n",
      "商品序号 (1): 蜜桃四季春 10元\n",
      "商品序号 (2): 芒果奶盖 12元\n",
      "商品序号 (3): 葡萄波波奶茶 8元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有88元\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蜜雪柠檬水 6元\n",
      "商品序号 (1): 蜜桃四季春 10元\n",
      "商品序号 (2): 芒果奶盖 12元\n",
      "商品序号 (3): 葡萄波波奶茶 8元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有76元\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蜜雪柠檬水 6元\n",
      "商品序号 (1): 蜜桃四季春 10元\n",
      "商品序号 (2): 芒果奶盖 12元\n",
      "商品序号 (3): 葡萄波波奶茶 8元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...您的余额还有68元\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蜜雪柠檬水 6元\n",
      "商品序号 (1): 蜜桃四季春 10元\n",
      "商品序号 (2): 芒果奶盖 12元\n",
      "商品序号 (3): 葡萄波波奶茶 8元\n",
      "请输入商品编号：q\n",
      "您已购买的商品: [['芒果奶盖', 12], ['芒果奶盖', 12], ['葡萄波波奶茶', 8]]\n",
      "您的余额还有68,欢迎下次光临~~~\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "   [ \"蜜雪柠檬水\",6],\n",
    "   [\"蜜桃四季春\",10],\n",
    "   [\"芒果奶盖\",12],\n",
    "   [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "shopping_car = []\n",
    "\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"--------------商品列表--------------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\"):\" ,product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "\n",
    "    # 2.让用户输入商品编号进行购买：直接用商品列表的index作为商品编号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景一：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if Alipay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "       # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~\")\n",
    "    # 情景二：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品:\",shopping_car)\n",
    "        print(\"您的余额还有%s,欢迎下次光临~~~\"%(Alipay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fc01e387",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 假设用户选择了0\n",
    "product_list[0][1]\n",
    "# 用户选择的商品价格： product_list[user_choice][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65717f53",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
